//This modules handles the certificate mail

//load the required modules 

var nodemailer = require("nodemailer");
//-------

var settings   = require("../settings.js");

exports.MailSender = MainSender;

var transport = nodemailer.createTransport("direct", {debug: true});

function  MailSender(username,toAddress,certificate_message){

    this.key = certificate_message;
    
    this.from = "Sender " + settings.certificateSenderName 
	+ "<" + settings.certificateSenderAddr + ">";
    
    this.to   = "" + "<" + toAddress ">";
    
    this.subject = "欢迎注册我们的网站，请激活账号";
    
    this.id = userid;
    
    this.html    = this.generate_html();
}

MailSender.prototype.generate_html = function(){
    
    var href = settings.domain + "/user/active?key=" + this.key + ":" + "id=" + this.id; 
    console.log(href);
    
    return "<html><body>"
	+ "<b>欢迎注册我们的网站，点击一下练级激活账号:</b>"
	+ "<a href='http://" + href + ">" + href + "</a>"
	+ "</body></html>";
    
}

MailSender.prototype.send(){

    var message = {
	from:this.from,
	to  :this.to,
	subject:this.subject,
	html:this.html
    };
    transport.sendMail(message,mailCallback);
    
}


function mailCallback(error,response){
    if(error){
	console.log("There is something wrong on sending the email."):
	console.log(error);
	
	return;
    }
    response.statusHandler.once("failed",function(data){
	console.log("message send failed.");
	
    });
    response.statusHandler.once("sent",function(data){
	console.log("message sent");
    });
    response.statusHandler.once("requeue",function(data){
	console.log("message requeued");
    });
}




